﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galoap
{
    partial class PhratrisQuestion : PhraseQuestion
    {

        /// <summary>
        /// String representing the visit of the tree
        /// </summary>
        public string[] TreeString { get; set; }

        
        /// <summary>
        /// void constructor of PhratrisQuestion
        /// </summary>
        public PhratrisQuestion()
            : base()
        {
            TextBuilder = new QuestionText("Posiziona correttamente le parole nella frase.");
            Text = TextBuilder.GetQuestionText();
            TreeString = new string[10];
            TreeOntology = new Ontology[10][];
            Tree = new Tree[10];

        }


        /// <summary>
        /// Take the semantic tree of the phrase; 
        /// than return the string of index of word and subphrases in the order of visualization in the game.
        /// </summary>
        /// <param name="tree">The semantic tree of the phrase</param>
        /// <returns>string of words in order of visualization in the game</returns>
        public string VisitTanlTree(Tree t)
        {
            StringBuilder response = new StringBuilder();
            t.SetSubPhrases();
            //calculate the deepest child
            int child_index = -1;
            Tree deepest = t.GetDeepestChild();
            while (deepest != null)
            {
                if (!deepest.Visited)
                {
                    if (deepest.Child.Length == 0)
                    {
                        response.Append(deepest.Index + ",");
                    }
                    else
                    {
                        response.Append(deepest.Index + ",\"" + Print(deepest.SubPhrases[child_index]) + "\",");
                    }
                    deepest.Visited = true;
                }
                else
                {
                    if (!(deepest.Child[child_index].Child.Length == 0 && PartOfSpeech[deepest.Child[child_index].Index - 1].Object.Equals("F")))
                    {
                        if (deepest.Child[child_index].Child.Length == 0 && deepest.Child.Length > child_index + 1 && deepest.Child[child_index + 1].Phrases[0] == deepest.Index + 1)
                            //if we came from a leaf and the next child of the node is consecutive in the phrase than we join the two subphrase
                            response.Append("\"" + Print(deepest.SubPhrases[child_index]) + " " + Print(deepest.Child[child_index + 1].Phrases) + "\",");
                        else
                            response.Append("\"" + Print(deepest.SubPhrases[child_index]) + "\",");
                    }
                    else
                        if (deepest.Parent == null && deepest.Child[child_index].Index == PartOfSpeech.Length)
                            response.Append("\"" + Print(deepest.Phrases) + "\",");
                }
                if (deepest.HasChildToVisit())
                {
                    deepest = deepest.GetDeepestChild();
                }
                else
                {
                    child_index = deepest.Child_index;
                    deepest = deepest.Parent;
                }
            }
            return response.ToString().Substring(0, response.Length - 1);
        }

        /// <summary>
        /// Convert an array of index of the phrase in the corrispectively subject of the ontology l
        /// </summary>
        /// <param name="substring">array of index of word in the phrase</param>
        /// <returns>the substring for that phrase</returns>
        private string Print(int[] substring)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < substring.Length; i++)
                sb.Append(PartOfSpeech[substring[i] - 1].Subject + " ");
            return sb.ToString();
        }

        /// <summary>
        /// This method initializes the question creating or setting the instance variables
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            for (int i = 0; i < Tree.Length; i++)
                TreeString[i] = VisitTanlTree(Tree[i]);
        }

        /// <summary>
        /// this method checks if the answer is an int number form 0 to 9, included
        /// </summary>
        /// <param name="reply">the string which is to validate</param>
        /// <returns>true if the answer in valid, false otherwise</returns>
        public override bool Validate(string reply)
        {
            int answer = 1;
            try
            {
                answer = System.Convert.ToInt32(reply, 10);
            } catch (Exception)
            {
                return false;
            }
            return base.Validate(reply) && 0 <= answer && answer <= 9;

        }



 
    }
}
